/**
    算法描述：
    核心问题：
    解决所需数据结构+算法：
**/
#include<bits/stdc++.h>
using namespace std;
int f[150010] = {0};
 
int findfa(int x) {
    return f[x] = f[x] == x ? x : findfa(f[x]);
}
 
int main() {
    int n, m, ans = 0;
    scanf("%d%d", &n, &m);
    for (int i=1; i<=3*n; i++)
        f[i] = i;
    while(m--) {
        int d, x, y, lx, lxo, lxt, ly, lyo, lyt;
        scanf("%d%d%d", &d, &x, &y);
        if (x>n || y>n) {
            ans++;
            continue;
        }
        if (d==2 && x==y) {
            ans++;
            continue;
        }
        lx = findfa(x);
        ly = findfa(y);
        lxo = findfa(x+n);
        lxt = findfa(x+2*n);
        lyo = findfa(y+n);
        lyt = findfa(y+2*n);
        if (d == 1) {
            if (lx==lyo || lx==lyt)
                ans++;
            else {
                f[lx] = ly;
                f[lxo] = lyo;
                f[lxt] = lyt;
            }
        } else {
            if (lx==ly || lx == lyt)
                ans++;
            else {
                f[lx] = lyo;
                f[lxo] = lyt;
                f[lxt] = ly;
            }
        }
    }
//  for (int i=1; i<=3*n; i++)
//      f[i] = findfa(i);
//  sort(f+1, f+2*n+1);
//  for (int i=2; i<=n; i++)
//      if(f[i]!=f[i-1])
//          ans++;
    printf("%d", ans);
    return 0;
}